
package com.compuvest.webstat;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// </editor-fold>

public class ProxyWeb extends HttpServlet {

// <editor-fold defaultstate="collapsed" desc="init()">

    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        String id = request.getParameter("id");
	if(id != null) {
            StringBuilder sb = new StringBuilder();
            URLConnection conn = null;
            BufferedReader rdr = null;

            if("com".equals(id)) {
                URL url = new URL("http://10.0.200.102/webinfo/webstatus?id=0d33c0c15a4838dde681654new");
                conn = url.openConnection();
                conn.setDoInput(true);
                conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11");

                rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = "";
                while((line = rdr.readLine()) != null) {
                    sb.append(line);
                }
            }
            else if("b2b".equals(id)) {
                URL url = new URL("http://10.0.200.108/webinfo/webstatus?id=0d33c0c15a4838dde68165434188");
                conn = url.openConnection();
                conn.setDoInput(true);
                conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11");

                rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = "";
                while((line = rdr.readLine()) != null) {
                    sb.append(line);
                }
            }

            PrintWriter out = response.getWriter();
            try {
                out.print(sb.toString());
/*            out.print(
                    "{'active':0," +
                    "'todayNewCust':42," +
                    "'todayOldCust':-3," +
                    "'todayVisit':60377," +
                    "'todayOrder':49," +
                    "'totalCust':303390," +
                    "'totalVisit':162769249," +
                    "'totalOrder':246851," +
                    "'notFillForm':3," +
                    "'yesFillForm':7," +
                    "'reseller':0," +
                    "adv:[{h:'compuvest',v:52912,o:19,a:3935.84,u:0}]" +
//                    "adv:{{n:'grabber',v:85,o:0,a:0.0},{n:'google19',v:62,o:0,a:0.0},{n:'google18',v:11,o:0,a:0.0},{n:'bing',v:196,o:1,a:457.41},{n:'buyedge',v:5,o:0,a:0.0},{n:'google25',v:24,o:0,a:0.0},{n:'google26',v:4,o:0,a:0.0},{n:'bizrate',v:20,o:0,a:0.0},{n:'google27',v:8,o:0,a:0.0},{n:'google28',v:603,o:0,a:0.0},{n:'google21',v:6,o:0,a:0.0},{n:'google22',v:169,o:0,a:0.0},{n:'onetime',v:4,o:0,a:0.0},{n:'google23',v:15,o:0,a:0.0},{n:'yahoo',v:8,o:0,a:0.0},{n:'google06',v:147,o:0,a:0.0},{n:'google05',v:13,o:0,a:0.0},{n:'google04',v:5,o:0,a:0.0},{n:'google29',v:52,o:0,a:0.0},{n:'google03',v:31,o:0,a:0.0},{n:'google02',v:15,o:0,a:0.0},{n:'google01',v:226,o:0,a:0.0},{n:'google09',v:180,o:0,a:0.0},{n:'froogle',v:2104,o:20,a:997.77},{n:'google08',v:6,o:0,a:0.0},{n:'google07',v:107,o:0,a:0.0},{n:'cnet',v:2,o:0,a:0.0},{n:'compuvest',v:52912,o:19,a:3935.84},{n:'nexttag',v:469,o:3,a:654.46},{n:'google30',v:44,o:0,a:0.0},{n:'google31',v:77,o:0,a:0.0},{n:'google34',v:24,o:0,a:0.0},{n:'google16',v:39,o:0,a:0.0},{n:'google35',v:140,o:0,a:0.0},{n:'google17',v:4,o:0,a:0.0},{n:'cj',v:1805,o:1,a:472.84},{n:'google32',v:22,o:0,a:0.0},{n:'google14',v:81,o:0,a:0.0},{n:'google33',v:25,o:0,a:0.0},{n:'google15',v:40,o:0,a:0.0},{n:'shopping',v:498,o:6,a:2466.0},{n:'google13',v:69,o:0,a:0.0},{n:'google10',v:15,o:0,a:0.0},{n:'google11',v:10,o:0,a:0.0}}" +
                    "}");*/
            }
            finally {
                out.close();
            }
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "ProxyWeb";
    }

    @Override
    public void destroy() {
    }
// </editor-fold>

}
